#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "..\\include\cfg_items.h"
#include "conf_loader.h"
#include "config_data.h"
#include "resource.h"

/*
W elfie wykorzystane sa fragmenty kodow: 
elfa "snow" dla tel. siemensa
"Standby_Example" z svn blacklizard
pomocne byly rowniez informacje usera diezil zamieszczone na forum se developers
*/
BOOK * RLBook;

DISP_OBJ * GUI_display=0;
DISP_DESC * Display_desc;
DISP_OBJ_ONREDRAW_METHOD Display_oldReDraw;

DISP_OBJ ** GUI_status=0;
DISP_DESC * Status_desc;
DISP_OBJ_ONREDRAW_METHOD Status_oldReDraw;

DISP_OBJ_ONREDRAW_METHOD Soft_oldReDraw;
DISP_OBJ * GUI_soft=0;
DISP_DESC * Soft_desc;

u16 ftimer;
STRID cpuID=0x6FFFFFFF;

int StatusBarY,SoftBarY,DisplayWidth,DisplayHeight;
typedef struct
{
	wchar_t ImageID;
	u16 ImageHandle;
	int img_width;
	int img_height;
}IMG;

wchar_t IDintflake; wchar_t IDintflake_m;
IMG image[6];IMG image_m[6];
int disref;int flag1=0;
int ix1,iy1,ix2,iy2,ix3,iy3,ix4,iy4,ix5,iy5;
int sw=0;
GVI_BRUSH m_br;
GVI_GC flake1_gvi= NULL; GVI_GC flake1_gvi_m= NULL;
GVI_GC flake2_gvi= NULL; GVI_GC flake2_gvi_m= NULL;
GVI_GC flake3_gvi= NULL; GVI_GC flake3_gvi_m= NULL;
GVI_GC flake4_gvi= NULL; GVI_GC flake4_gvi_m= NULL;
GVI_GC flake5_gvi= NULL; GVI_GC flake5_gvi_m= NULL;
GC*   flake1_gc; GC*   flake1_gc_m;
GC*   flake2_gc; GC*   flake2_gc_m;
GC*   flake3_gc; GC*   flake3_gc_m;
GC*   flake4_gc; GC*   flake4_gc_m;
GC*   flake5_gc; GC*   flake5_gc_m;
GC *context=0;

int widhei=0;
enum GVI_BLIT_RASTER {
        BLACKNESS=0x00,
        NOTSRCERASE=0x11,
        B_UNK_2=0x22,
        NOTSRCCOPY=0x33,
        SRCERASE=0x44,
        DSTINVERT=0x55,
        SRCINVERT=0x66,
        B_UNK_7=0x77,
        SRCAND=0x88,
        B_UNK_9=0x99,
        B_UNK_10=0xAA,
        MERGEPAINT=0xBB,
        SRCCOPY=0xCC,
        B_UNK_13=0xDD,
        SRCPAINT=0xEE,
        WHITENESS=0xFF
};
const int RASTER[]={BLACKNESS,NOTSRCERASE,B_UNK_2,NOTSRCCOPY,SRCERASE,DSTINVERT,SRCINVERT,B_UNK_7,SRCAND,B_UNK_9,B_UNK_10,MERGEPAINT,SRCCOPY,B_UNK_13,SRCPAINT,WHITENESS};

#define maxisnow 1024
typedef unsigned char byte;
unsigned short snow[maxisnow];
byte type[maxisnow];
unsigned short imag[maxisnow];
#define maxtime 10
const int time[maxtime]={10,30,60,90,120,180,250,500,1500,3000};
int timenum=1;
int maxsnow=0;

int vtime=0; //wiatr 0 -n +n
int sgf=0;
char *gf=0;

unsigned int randseed;

#define random randint
#define randomize Randomize
unsigned int randint( int max )
{
	randseed = randseed * 0x08088405;
	randseed++;
	return ((long long)randseed * max ) >> 32;
}

int rand(){
	return randint(RAND);
}

void Randomize(){
	DATETIME dt;
	REQUEST_DATEANDTIME_GET(SYNC, &dt);
	randseed=(dt.time.min | (dt.time.sec<<5));
}

u16 RegisterImage(IMG * i , wchar_t * path, wchar_t * fname)
{    char error_code;
	i->img_width = 0;
	i->img_height = 0;
	i->ImageID=0xFFFF;
	i->ImageHandle=0xFFFF;
	if (!fstat(path,fname,0))
	{
		if (!REQUEST_IMAGEHANDLER_INTERNAL_GETHANDLE(SYNC,&i->ImageHandle,&error_code))
		{
			if (!REQUEST_IMAGEHANDLER_INTERNAL_REGISTER(SYNC,i->ImageHandle,path,fname,0,&i->ImageID,&error_code))
			{
				if (error_code)
				{
					i->ImageHandle=0xFFFF;
				}
			}
		}
	}

	return(i->ImageID);
};

typedef struct
{
	BOOK * book;
}MSG;

void initsnow(){
	int i;
	unsigned int j;
	for(i=0;i<maxsnow;i++){
		j=rand()|(rand()<<8);
		while(j>=widhei) j-=widhei;
		snow[i]=j?j:1;
		j=rand();
		type[i]=j&types;//maxtype
		j=randint(6);
		imag[i]=randint(images);
	}
}

void initmem()
{
	m_br=GVI_CreateSolidBrush(Color_br);
	if(image[0].ImageID<0xFFFF)  // okreslenie wymiarow obrazka
	{ix1=GetImageWidth(image[0].ImageID);
		iy1=GetImageHeight(image[0].ImageID);}
	else {ix1=7; iy1=9;}
	flake1_gc = GC_CreateMemoryGC( ix1, iy1, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake1_gc->pcanvas ,&flake1_gvi);
	GVI_FillRectangle(flake1_gvi, 0,0, ix1,iy1, m_br);
	if(image[0].ImageID<0xFFFF) { putchar(flake1_gc, 0,0, 0,0,   image[0].ImageID); }
	else
		putchar(flake1_gc,0,0,0,0, IDintflake);

	flake1_gc_m = GC_CreateMemoryGC( ix1, iy1, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake1_gc_m->pcanvas ,&flake1_gvi_m);
	GVI_FillRectangle(flake1_gvi_m, 0,0, ix1,iy1, m_br);
if(image_m[0].ImageID<0xFFFF) { putchar(flake1_gc_m, 0,0, 0,0,   image_m[0].ImageID); }
	else
		putchar(flake1_gc_m,0,0,0,0, IDintflake_m);

	if(image[1].ImageID<0xFFFF)  // okreslenie wymiarow obrazka
	{ix2=GetImageWidth(image[1].ImageID);
		iy2=GetImageHeight(image[1].ImageID);}
	else {ix2=7; iy2=9;}
	flake2_gc = GC_CreateMemoryGC( ix2, iy2, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake2_gc->pcanvas ,&flake2_gvi);
	GVI_FillRectangle(flake2_gvi, 0,0, ix2,iy2, m_br);
	if(image[1].ImageID<0xFFFF) { putchar(flake2_gc, 0,0, 0,0,   image[1].ImageID); }
	else
		putchar(flake2_gc,0,0,0,0, IDintflake);

	flake2_gc_m = GC_CreateMemoryGC( ix2, iy2, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake2_gc_m->pcanvas ,&flake2_gvi_m);
	GVI_FillRectangle(flake2_gvi_m, 0,0, ix2,iy2, m_br);
if(image_m[1].ImageID<0xFFFF) { putchar(flake2_gc_m, 0,0, 0,0,   image_m[1].ImageID); }
	else
		putchar(flake2_gc_m,0,0,0,0, IDintflake_m);

	if(image[1].ImageID<0xFFFF)  // okreslenie wymiarow obrazka
	{ix3=GetImageWidth(image[2].ImageID);
		iy3=GetImageHeight(image[2].ImageID);}
	else {ix3=7; iy3=9;}
	flake3_gc = GC_CreateMemoryGC( ix3, iy3, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake3_gc->pcanvas ,&flake3_gvi);
	GVI_FillRectangle(flake3_gvi, 0,0, ix3,iy3, m_br);
	if(image[2].ImageID<0xFFFF) { putchar(flake3_gc, 0,0, 0,0,   image[2].ImageID); }
	else
		putchar(flake3_gc,0,0,0,0, IDintflake);

	flake3_gc_m = GC_CreateMemoryGC( ix3, iy3, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake3_gc_m->pcanvas ,&flake3_gvi_m);
	GVI_FillRectangle(flake3_gvi_m, 0,0, ix3,iy3, m_br);
if(image[2].ImageID<0xFFFF) { putchar(flake3_gc_m, 0,0, 0,0,   image_m[2].ImageID); }
	else
		putchar(flake3_gc_m,0,0,0,0, IDintflake_m);

	if(image[3].ImageID<0xFFFF)  // okreslenie wymiarow obrazka
	{ix4=GetImageWidth(image[3].ImageID);
		iy4=GetImageHeight(image[3].ImageID);}
	else {ix4=7; iy4=9;}
	flake4_gc = GC_CreateMemoryGC( ix4, iy4, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake4_gc->pcanvas ,&flake4_gvi);
	GVI_FillRectangle(flake4_gvi, 0,0, ix4,iy4, m_br);
	if(image[3].ImageID<0xFFFF) { putchar(flake4_gc, 0,0, 0,0,   image[3].ImageID); }
	else
		putchar(flake4_gc,0,0,0,0, IDintflake);

	flake4_gc_m = GC_CreateMemoryGC( ix4, iy4, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake4_gc_m->pcanvas ,&flake4_gvi_m);
	GVI_FillRectangle(flake4_gvi_m, 0,0, ix4,iy4, m_br);
if(image_m[3].ImageID<0xFFFF) { putchar(flake4_gc_m, 0,0, 0,0,   image_m[3].ImageID); }
	else
		putchar(flake4_gc_m,0,0,0,0, IDintflake_m);

	if(image[4].ImageID<0xFFFF)  // okreslenie wymiarow obrazka
	{ix5=GetImageWidth(image[4].ImageID);
		iy5=GetImageHeight(image[4].ImageID);}
	else {ix5=7; iy5=9;}
	flake5_gc = GC_CreateMemoryGC( ix5, iy5, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake5_gc->pcanvas ,&flake5_gvi);
	GVI_FillRectangle(flake5_gvi, 0,0, ix5,iy5, m_br);
	if(image[4].ImageID<0xFFFF) { putchar(flake5_gc, 0,0, 0,0,   image[4].ImageID); }
	else
		putchar(flake5_gc,0,0,0,0, IDintflake);

	flake5_gc_m = GC_CreateMemoryGC( ix5, iy5, 16, 0,0,0 );
	CANVAS_Get_GviGC( flake5_gc_m->pcanvas ,&flake5_gvi_m);
	GVI_FillRectangle(flake5_gvi_m, 0,0, ix5,iy5, m_br);
if(image_m[4].ImageID<0xFFFF) { putchar(flake5_gc_m, 0,0, 0,0,   image_m[4].ImageID); }
	else
		putchar(flake5_gc_m,0,0,0,0, IDintflake_m);

	sw=1;
}

void DrawParams(int y ,int z)
{
	if (!sw) initmem();
	GVI_GC _gcx = NULL;
	CANVAS_Get_GviGC(context->pcanvas, &_gcx);
	int i,j,x;
	++vtime;
	for(i=0;i<maxsnow;i++)
	{
		if(z)
		{
			switch(type[i]){ //typy spadania
			case 0: x=width*winds1; break;//zwykle spadanie
			case 1: x=width*winds2; break; //srednia predkosc
			case 2: x=width*winds3; break; //wysoka
			case 3: x=width*winds2+((vtime&windp1)?1:-1); break; // z wiatrem
			case 4: x=width+((vtime&windp2)?1:-1); break;
			case 5: x=width-((vtime&windp3)?1:-1); break;
			case 6: x=width*winds3-((vtime&windp2)?1:-1); break;
				// case 7: x=width*(winds1/2); break; //polowa exp.
			default: x=width+((vtime&windp3)?1:-1); break;
			}
			if((snow[i]+=(x))>=widhei){
				snow[i]=rand()+1;
				j=rand();
				type[i]=j&types;//maxtype
				imag[i]=randint(images);
			}
		}
		if(mask) {
			if(imag[i]==0)    { GVI_BitBlt(_gcx,
				                               snow[i]%width-ix1, (snow[i]/width)+y,
				                               ix1, iy1,
				                               flake1_gvi_m,
				                               0, 0,
				                               SRCAND, 0,  2,  3);
			}
			if(imag[i]==1)    { GVI_BitBlt(_gcx,
				                               snow[i]%width-ix1, (snow[i]/width)+y,
				                               ix2, iy2,
				                               flake2_gvi_m,
				                               0, 0,
				                               SRCAND, 0,  2,  3);
			}
			if(imag[i]==2)    { GVI_BitBlt(_gcx,
				                               snow[i]%width-ix1, (snow[i]/width)+y,
				                               ix3, iy3,
				                               flake3_gvi_m,
				                               0, 0,
				                               SRCAND, 0,  2,  3);
			}
			if(imag[i]==3)    { GVI_BitBlt(_gcx,
				                               snow[i]%width-ix1, (snow[i]/width)+y,
				                               ix4, iy4,
				                               flake4_gvi_m,
				                               0, 0,
				                               SRCAND, 0,  2,  3);
			}
			if(imag[i]==4)    { GVI_BitBlt(_gcx,
				                               snow[i]%width-ix1, (snow[i]/width)+y,
				                               ix5, iy5,
				                               flake5_gvi_m,
				                               0, 0,
				                               SRCAND, 0,  2,  3);
			}
		}
		if(imag[i]==0)    { GVI_BitBlt(_gcx,
			                               snow[i]%width-ix1, (snow[i]/width)+y,
			                               ix1, iy1,
			                               flake1_gvi,
			                               0, 0,
			                               RASTER[RASTN], 0,  2,  3);

		}
		if(imag[i]==1)    { GVI_BitBlt(_gcx,
			                               snow[i]%width-ix1, (snow[i]/width)+y,
			                               ix2, iy2,
			                               flake2_gvi,
			                               0, 0,
			                               RASTER[RASTN], 0,  2,  3);
		}
		if(imag[i]==2)    { GVI_BitBlt(_gcx,
			                               snow[i]%width-ix1, (snow[i]/width)+y,
			                               ix3, iy3,
			                               flake3_gvi,
			                               0, 0,
			                               RASTER[RASTN], 0,  2,  3);
		}
		if(imag[i]==3)    { GVI_BitBlt(_gcx,
			                               snow[i]%width-ix1, (snow[i]/width)+y,
			                               ix4, iy4,
			                               flake4_gvi,
			                               0, 0,
			                               RASTER[RASTN], 0,  2,  3);
		}
		if(imag[i]==4)    { GVI_BitBlt(_gcx,
			                               snow[i]%width-ix1, (snow[i]/width)+y,
			                               ix5, iy5,
			                               flake5_gvi,
			                               0, 0,
			                               RASTER[RASTN], 0,  2,  3);
		}
	}
}

void Display_ReDraw(DISP_OBJ * DO, int a, int b, int c)
{
	if(IS_ENABLED_sb) DrawParams(-(StatusBarY-1),0);  else    {DrawParams(-(StatusBarY-1),1);}
	if(!clearsb)  if (Display_oldReDraw) Display_oldReDraw(DO, a, b, c);
}

void Status_ReDraw(DISP_OBJ * DO, int a, int b, int c)
{
	if(IS_ENABLED_sb) { DrawParams(0,1);} else DrawParams(0,0);
	if(!clearsb) Status_oldReDraw(DO, a, b, c);
}

void Soft_ReDraw(DISP_OBJ* DO, int a, int b, int c)
{
	DrawParams(-sbpoz,0);
	if(!clearsb) Soft_oldReDraw(DO, a, b, c);
}

void Send_REDRAW_RELEASE()
{
	REDRAW_RELEASE_MESSAGE * sbm = new REDRAW_RELEASE_MESSAGE;
	sbm->SB_OldOnRedraw = Display_oldReDraw;
	sbm->SB_NewOnRedraw = Display_ReDraw;
	sbm->SI_OldOnRedraw = Status_oldReDraw;
	sbm->SI_NewOnRedraw = Status_ReDraw;
	sbm->SK_OldOnRedraw = Soft_oldReDraw;
	sbm->SK_NewOnRedraw = Soft_ReDraw;
	UI_Event_wData(SBY_REDRAW_RELEASE_EVENT, sbm, (void (*)(void*))mfree_adr());
}

int ShowAuthorInfo(void *mess , BOOK* book)
{
	MSG * msg = (MSG*)mess;
	wchar_t info[256];
	snwprintf(info, 255, L"Snow 0.81\n(c) zylka\n%s\n%s", __DATE__, __TIME__);
	MessageBox(EMPTY_SID, Str2ID(info, 0, SID_ANY_LEN), NOIMAGE, 1 , 0, msg->book);
	return(1);
}

int SB_ELF_Killed(void *mess , BOOK* book)
{
	REDRAW_RELEASE_MESSAGE * sbm = (REDRAW_RELEASE_MESSAGE*)mess;
	REDRAW_RELEASE_MESSAGE * ms = 0;
	if (sbm->SI_NewOnRedraw == Status_oldReDraw)
	{
		ms = new REDRAW_RELEASE_MESSAGE;
		memcpy(ms, sbm, sizeof(REDRAW_RELEASE_MESSAGE));
		if (sbm->SI_OldOnRedraw != EMPTY_REDRAW_METHOD) Status_oldReDraw = sbm->SI_OldOnRedraw;
		DISP_DESC_SetOnRedraw(DispObject_GetDESC(*GUI_status), Status_ReDraw);
		ms->SI_OldOnRedraw = EMPTY_REDRAW_METHOD;
		ms->SI_NewOnRedraw = Status_ReDraw;
	}
	if (sbm->SB_NewOnRedraw == Display_oldReDraw)
	{
		if (!ms)
		{
			ms = new REDRAW_RELEASE_MESSAGE;
			memcpy(ms, sbm, sizeof(REDRAW_RELEASE_MESSAGE));
		}
		if (sbm->SB_OldOnRedraw != EMPTY_REDRAW_METHOD) Display_oldReDraw = sbm->SB_OldOnRedraw;
		DISP_DESC_SetOnRedraw(DispObject_GetDESC(GUI_display), Display_ReDraw);
		ms->SB_OldOnRedraw = EMPTY_REDRAW_METHOD;
		ms->SB_NewOnRedraw = Display_ReDraw;
	}

	if (sbm->SK_NewOnRedraw == Soft_oldReDraw)
	{
		if (!ms)
		{
			ms = new REDRAW_RELEASE_MESSAGE;
			memcpy(ms, sbm, sizeof(REDRAW_RELEASE_MESSAGE));
		}
		if (sbm->SK_OldOnRedraw != EMPTY_REDRAW_METHOD) Soft_oldReDraw = sbm->SK_OldOnRedraw;
		DISP_DESC_SetOnRedraw(DispObject_GetDESC(GUI_soft), Soft_ReDraw);
		ms->SK_OldOnRedraw = EMPTY_REDRAW_METHOD;
		ms->SK_NewOnRedraw = Soft_ReDraw;
	}
	if (ms)
	{
		UI_Event_wData(SBY_REDRAW_RELEASE_EVENT , ms, (void (*)(void*))mfree_adr());
		return(BLOCK_EVENT_GLOBALLY);
	}
	return(0);
}

int TerminateElf(void * , BOOK * book)
{
	FreeBook(book);
	return(1);
}

static int ReconfigElf(void *mess ,BOOK *book)
{
	RECONFIG_EVENT_DATA *reconf=(RECONFIG_EVENT_DATA *)mess;
	int result=0;
	if (wstrcmpi(reconf->path,successed_config_path)==0 && wstrcmpi(reconf->name,successed_config_name)==0)
	{
		InitConfig();
		widhei=width*height;
		result=1;
	}
	return(result);
}

int onBcfgConfig(void* mess,BOOK* b)
{
	FSTAT _fstat;
	wchar_t path[256];

	if(fstat(GetDir(DIR_ELFS|MEM_INTERNAL),L"BcfgEdit.elf",&_fstat)==0)
	{
		wstrcpy(path,GetDir(DIR_ELFS|MEM_INTERNAL));
	}
	else if(fstat(GetDir(DIR_ELFS|MEM_EXTERNAL),L"BcfgEdit.elf",&_fstat)==0)
	{
		wstrcpy(path,GetDir(DIR_ELFS|MEM_EXTERNAL));
	}
	else
	{
		MessageBox(0x6FFFFFFF, STR("Elf bcfgeditor nie znaleziony!"), 0, 1 ,5000, 0);
		return (1);
	}
	wstrcat(path,L"/BcfgEdit.elf");
	elfload(path,(void*)successed_config_path,(void*)successed_config_name,0);
	return (1);
}

#define ELF_BCFG_CONFIG_EVENT 994

const PAGE_MSG HW_PageEvents[]@ "DYN_PAGE" = {
                        ELF_TERMINATE_EVENT ,  TerminateElf,
                        ELF_SHOW_INFO_EVENT  , ShowAuthorInfo,
                        ELF_RECONFIG_EVENT,    ReconfigElf,
                        ELF_BCFG_CONFIG_EVENT, onBcfgConfig,    // uruchomienie konfiguracji elfa przez bookmanageramod
                        NIL_EVENT_TAG,         NULL
                };

PAGE_DESC base_page = {"snow_BasePage", 0, HW_PageEvents};

void elf_exit(void)
{
	kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}

void onfTimer (u16 tmr , void *)
{
	if (GUI_display)
		DispObject_InvalidateRect(GUI_display, 0);
	if (GUI_status)
		DispObject_InvalidateRect(*GUI_status, 0);
	if (GUI_soft)
		DispObject_InvalidateRect(GUI_soft, 0);
	if(timem)  Timer_ReSet(&ftimer,timem,onfTimer,0); else Timer_ReSet(&ftimer,time[timenum],onfTimer,0);
}

int NewKey(int key, int r1, int mode)
{
	if(control)
	{
		BOOK * standby = DISPLAY_GetTopBook(0);
		if ( (!strcmp(standby->xbook->name,"StandbyBook")) || (!strcmp(standby->xbook->name,"ShortcutBook")) )
		{
			if(key==KEY_UP && mode==0x00)
			{
				if((maxsnow+=stepsnow)>maxisnow) maxsnow=maxisnow;
				initsnow();return(-1);
			}
			if(key==KEY_DOWN&&mode==0x00)
			{
				if((maxsnow-=stepsnow)<stepsnow) maxsnow=stepsnow;
				initsnow();return(-1);
			}
			if(key==KEY_LEFT&&mode==0x00)
			{
				if(--timenum<0) timenum=0;return(-1);
			}
			if(key==KEY_RIGHT&&mode==0x00)
			{
				if(++timenum>=maxtime) timenum=maxtime-1;return(-1);
			}
		}
	}
	return(0);
}

void onCloseRLBook(BOOK * book)
{
	if (book)
	{
		if (Display_desc)
			DISP_DESC_SetOnRedraw(Display_desc, Display_oldReDraw);
		if (Status_desc)
			DISP_DESC_SetOnRedraw(Status_desc, Status_oldReDraw);
		if (Soft_desc)
			DISP_DESC_SetOnRedraw(Soft_desc, Soft_oldReDraw);
		Timer_Kill(&ftimer);
		GVI_Delete_GVI_Object( &m_br );
		GC_FreeGC( flake1_gc );GC_FreeGC( flake1_gc_m );
		GC_FreeGC( flake2_gc );GC_FreeGC( flake2_gc_m );
		GC_FreeGC( flake3_gc );GC_FreeGC( flake3_gc_m );
		GC_FreeGC( flake4_gc );GC_FreeGC( flake4_gc_m );
		GC_FreeGC( flake5_gc );GC_FreeGC( flake5_gc_m );
		char error_code;
		for(int i=0;i<5;i++)
		{
			if(!image[i].ImageID<0xFFFF) REQUEST_IMAGEHANDLER_INTERNAL_UNREGISTER(SYNC,image[i].ImageHandle,0,0,image[i].ImageID,1,&error_code);
			if(!image_m[i].ImageID<0xFFFF)REQUEST_IMAGEHANDLER_INTERNAL_UNREGISTER(SYNC,image_m[i].ImageHandle,0,0,image_m[i].ImageID,1,&error_code);
		}
		// ImageID_Free(IDintflake);
		ModifyKeyHook(NewKey,0);
		SUBPROC(elf_exit);
	}
}

BOOK * CreateRLBook()
{
	RLBook = new(BOOK);
	CreateBook(RLBook, onCloseRLBook, &base_page, "Snow", -1, 0);
	return(RLBook);
}

int myFind(BOOK* book)
{
	if (!strcmp(book->xbook->name, "Snow"))
		return(1);
	return(0);
}

void reg()
{
	RegisterImage(& image[0] ,(wchar_t*)folder,(wchar_t*)L"snow1.png");
	RegisterImage(& image[1] ,(wchar_t*)folder,(wchar_t*)L"snow2.png");
	RegisterImage(& image[2] ,(wchar_t*)folder,(wchar_t*)L"snow3.png");
	RegisterImage(& image[3] ,(wchar_t*)folder,(wchar_t*)L"snow4.png");
	RegisterImage(& image[4] ,(wchar_t*)folder,(wchar_t*)L"snow5.png");

	RegisterImage(& image_m[0] ,(wchar_t*)folder,(wchar_t*)L"snow1_mask.png");
	RegisterImage(& image_m[1] ,(wchar_t*)folder,(wchar_t*)L"snow2_mask.png");
	RegisterImage(& image_m[2] ,(wchar_t*)folder,(wchar_t*)L"snow3_mask.png");
	RegisterImage(& image_m[3] ,(wchar_t*)folder,(wchar_t*)L"snow4_mask.png");
	RegisterImage(& image_m[4] ,(wchar_t*)folder,(wchar_t*)L"snow5_mask.png");

	ImageID_GetIndirect ( snow1, 136, 0,L"bmp",&IDintflake);
	ImageID_GetIndirect ( snow2, 136, 0,L"bmp",&IDintflake_m);
}

int main (void)
{
	if (FindBook(myFind))
	{
		int iconID;
		iconidname2id(L"POPUP_BIG_WARNING_ICN", -1, &iconID);
		MessageBox(EMPTY_SID, STR("Already runed"), iconID, 1, 5000, 0);
		SUBPROC(elf_exit);
	}
	else
	{
		InitConfig();
		DisplayWidth  = Display_GetWidth(0);
		DisplayHeight = Display_GetHeight(0);
		StatusBarY = DispObject_GetWindowHeight(*StatusRow_p());
		SoftBarY = DisplayHeight - DispObject_GetWindowHeight(DispObject_SoftKeys_Get());
		debug_printf("\n Zylka  stat.bar: %d soft.key %d  wys.ekr:%d\n",StatusBarY ,SoftBarY,DisplayHeight);
		CreateRLBook();
		ftimer = Timer_Set(100, onfTimer, 0);

		GUI_display = GUIObject_GetDispObject((GUI*)List_Get(Find_StandbyBook()->xguilist->guilist, 0));
		Display_oldReDraw = DISP_OBJ_GetOnRedraw(GUI_display);
		Display_desc = DispObject_GetDESC(GUI_display);
		DISP_DESC_SetOnRedraw(Display_desc, Display_ReDraw);

		if(IS_ENABLED_sb){
			GUI_status = StatusRow_p();
			Status_desc = DispObject_GetDESC (* GUI_status);
			Status_oldReDraw = DispObject_GetOnRedraw (* GUI_status);
			DISP_DESC_SetOnRedraw (Status_desc, (DISP_OBJ_ONREDRAW_METHOD)Status_ReDraw);}
		if(IS_ENABLED_sk){
			GUI_soft = DispObject_SoftKeys_Get();
			Soft_desc = DispObject_GetDESC (GUI_soft);
			Soft_oldReDraw = DispObject_GetOnRedraw(GUI_soft);
			DISP_DESC_SetOnRedraw(Soft_desc, (DISP_OBJ_ONREDRAW_METHOD)Soft_ReDraw);}
	}
	reg();
	maxsnow=maxsnow2;
	widhei=width*height;
	Randomize();
	initsnow();
	context = (GC *)get_DisplayGC();
	ModifyKeyHook(NewKey,1);

	return(0);
}

